package com.baosight.sczx.zm.cg.service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cg.DailyReport;
import com.baosight.sczx.common.zm.cInterface.cp.ProducePlanCM;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.zm.cg.domain.ZMCGAccident;
import com.baosight.sczx.zm.cg.domain.ZMCGGas;
import com.baosight.sczx.zm.ch.domain.ZMCHMineReport;

/**
 * 瓦斯抽采量及进尺月报
 * <p>Title: ServiceZMCH00.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 卢健
 * @version 1.0
 *</pre>
 */
public class ServiceZMCG99 extends ServiceEPBase {
	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP70</p>
	 * <p>throws Exception:</p>
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的		
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP70</p>
	 * <p>throws Exception:</p>
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdMiningId = (String)UserSession.getInSessionProperty("miningCode");
	    
//		StaticTools.setQueryParameters(inInfo, "fdMiningId", fdMiningId, true);
		EiInfo outInfo = super.query(inInfo, "ZMCGGas.query", new ZMCGGas());
		
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.ch.status", new String[]{"草拟", "上报"}, new String[]{"草拟", "上报"}, true));//状态
		outInfo.addBlock(StaticBlocks.getPlanMonth("cmes.cp.planMonth", true));
		outInfo.addBlock(getMines());
		return outInfo;
	}
	
	/**
	 * 矿井block
	 * @return
	 */
	private EiBlock getMines() {
		// TODO Auto-generated method stub
		EiBlock block = new EiBlock("cmes.cg.mine");

		EiColumn EiColumn = new EiColumn("display");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		EiColumn = new EiColumn("value");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		List<Map> _list = new ArrayList<Map>();
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("value", "");
		_map.put("display", "");
		_list.add(_map);

		List<Map> _listData = new ArrayList<Map>();
		try {
			_listData = dao.query("ZMCGGas.queryTreeChild", null);
		} catch (Exception e) {}
		
		for(int i = 0; i < _listData.size(); i++) {
			_list.add(_listData.get(i)); 
		} 
		block.setRows(_list);
		return block;
	}
	
	/**
	 * 新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP70</p>
	 * <p>throws Exception:</p>
	 * </p>
	 *
	 */ 
	@Override
	public EiInfo insert(EiInfo inInfo) {	
		EiInfo out = new EiInfo();
		ZMCGGas _dom = new ZMCGGas();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));

			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CG_GAS"));//主键
//			_dom.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());	//矿井id
//			_dom.setFdMiningName(UserSession.getInSessionProperty("miningName").toString());// 矿井名称
			_dom.setFdEditor(UserSession.getInSessionProperty("UserCname").toString());// 汇报人				
			
			boolean validate = this.validate(inInfo, _dom, "ZMCGGas.validate", new HashMap<String, String>());
			if(validate) {
				inInfo = this.query(inInfo);
				return inInfo;
			}
			this.dao.insert("ZMCGGas.insert", _dom);
		}
		
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}

	/**
	 * 修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP70</p>
	 * <p>throws Exception:</p>
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCGGas _dom = new ZMCGGas();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();

		for (int i = 0 ; i < rowcount; i++) {
				_dom.fromMap(eiBlock.getRow(i));

				_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CG_GAS"));//主键
				_dom.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());	//矿井id
				_dom.setFdMiningName(UserSession.getInSessionProperty("miningName").toString());// 矿井名称
				_dom.setFdEditor(UserSession.getInSessionProperty("UserCname").toString());// 汇报人				
				
				boolean validate = this.validate(inInfo, _dom, "ZMCGGas.validate", new HashMap<String, String>());
				if(validate) {
					inInfo = this.query(inInfo);
					return inInfo;
				}
				this.dao.update("ZMCGGas.update", _dom);
			}
		
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;	
	}		
			
	
	/**
	 * 删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP70</p>
	 * <p>throws Exception:</p>
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCGGas _dom = new ZMCGGas();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			this.dao.delete("ZMCGGas.delete", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   
	}
	
	/**
	 * 上报
	 * <p>Function: report</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * </p>
	 *
	 */
	public EiInfo report(EiInfo inInfo)
	{	
		EiInfo out=new EiInfo(); 
		ZMCGGas _dom = new ZMCGGas();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		
		Date date=new Date();
		DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom.setFdVirtualDate(date);
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
	
			this.dao.update("ZMCGGas.report", _dom);
		}	
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   
	}

	/**
	 * 撤回
	 * <p>Function: report</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * </p>
	 *
	 */
	public EiInfo revert(EiInfo inInfo)
	{	
		EiInfo out=new EiInfo(); 
		ZMCGGas _dom = new ZMCGGas();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
	
			this.dao.update("ZMCGGas.revert", _dom);
		}	
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   
	}

	/**
	 * 校验是否重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes" })
	private boolean validate(EiInfo inInfo, ZMCGGas _dom, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}

		params.put("fdYear", _dom.getFdYear());
		params.put("fdMiningId", _dom.getFdMiningId());
		params.put("fdMonth", _dom.getFdMonth());
		
		List result = dao.query("ZMCGGas.validate", params);
		
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			String msg = "每矿每月只能填写一份！";
			inInfo.setMsg(msg);
			return true;
		}
		return false;
	}	
}
